package com.chainyoung.trademarket.adapter;

import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;
import com.chainyoung.common.imageloader.glide.transformations.RoundedCornersTransformationNew;
import com.chainyoung.common.view.banner.BannerEntry;
import com.chainyoung.common.view.banner.view.BannerView;
import com.chainyoung.common.view.banner.view.PointIndicatorView;
import com.chainyoung.component_tmmarket.bean.TitleImageBannerEntry;
import com.chainyoung.component_tmperson.ui.activity.dapp.BrowserActivity;
import com.chainyoung.trademarket.R;
import com.chainyoung.trademarket.entity.FindItem;

import java.util.ArrayList;
import java.util.List;

import static com.chainyoung.common.constant.CommonStringConstants.URL;

public class FindAdapter extends RecyclerView.Adapter {


    private Context mContext;
    private List<FindItem> banners;
    private List<FindItem> list;


    private final static int TYPE_BANNER = 0;
    private final static int TYPE_LIST = 1;
    private LayoutInflater inflate;

    public FindAdapter(Context context, List<FindItem> bannerList, List<FindItem> list) {
        this.mContext = context;
        this.banners = bannerList;
        this.list = list;
        inflate = LayoutInflater.from(mContext);
    }

    @Override
    public int getItemViewType(int position) {
        if (position == 0) {
            return TYPE_BANNER;
        } else {
            return TYPE_LIST;
        }

    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        switch (viewType) {
            case TYPE_BANNER:
                View view = inflate.inflate(R.layout.layout_find_type_banner, parent, false);
                return new HeadHolder(view);
            case TYPE_LIST:
                view = inflate.inflate(R.layout.layout_find_list_item, parent, false);
                return new ListHolder(view);

        }
        return null;
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        switch (getItemViewType(position)) {
            case TYPE_BANNER:
                HeadHolder headHolder = (HeadHolder) holder;
                headHolder.banner.setEntries(getBannerEntries(banners), true);
                break;
            case TYPE_LIST:
                ListHolder listHolder = (ListHolder) holder;
                FindItem item = list.get(position - 1);
                listHolder.tvName.setText(item.getName());
                listHolder.tvDesc.setText(item.getIntro());
                Glide.with(mContext).load(item.getIcon()).apply(RequestOptions.bitmapTransform(new RoundedCornersTransformationNew(6, 0))).into(listHolder.ivLogo);
                listHolder.rlFind.setTag(item);
                break;
        }
    }

    @Override
    public int getItemCount() {
        return list.size() + 1;
    }


    class HeadHolder extends RecyclerView.ViewHolder {

        BannerView banner;
        PointIndicatorView bivIndicator;


        public HeadHolder(View itemView) {
            super(itemView);

            banner = itemView.findViewById(R.id.vp_view_pager);
            bivIndicator = itemView.findViewById(R.id.biv_indicator);
            banner.setIndicatorView(bivIndicator);
            banner.setOnPageClickListener(new BannerView.OnPageClickListener() {
                @Override
                public void onPageClick(BannerEntry entry, int index) {
                    FindItem item = banners.get(index);
                    if (!TextUtils.isEmpty(item.getPackage_name())) {
                        if (!startApp(item.getPackage_name())) {
                            launch(item.getUrl(), item.getName());
                        }
                    } else {
                        launch(item.getUrl(), item.getName());
                    }
                }
            });

        }
    }

    class ListHolder extends RecyclerView.ViewHolder implements View.OnClickListener {

        ImageView ivLogo;
        TextView tvName;
        TextView tvDesc;
        RelativeLayout rlFind;

        public ListHolder(View itemView) {
            super(itemView);
            ivLogo = itemView.findViewById(R.id.iv_logo);
            tvName = itemView.findViewById(R.id.tv_name);
            tvDesc = itemView.findViewById(R.id.tv_desc);
            rlFind = itemView.findViewById(R.id.rl_find);
            rlFind.setOnClickListener(this);
        }

        @Override
        public void onClick(View v) {
            FindItem item = (FindItem) v.getTag();
            if (!TextUtils.isEmpty(item.getPackage_name())) {
                if (!startApp(item.getPackage_name())) {
                    launch(item.getUrl(), item.getName());
                }
            } else {
                launch(item.getUrl(), item.getName());
            }


        }
    }

    @NonNull
    private List<TitleImageBannerEntry> getBannerEntries(List<FindItem> data) {
        List<TitleImageBannerEntry> items = new ArrayList<>();
        for (FindItem findItem : data) {
            items.add(new TitleImageBannerEntry(mContext, findItem.getName(), findItem.getIntro(), findItem.getBanner(), findItem.getUrl()));
        }
        return items;
    }

    private boolean startApp(String pkgName) {
        PackageManager packageManager = mContext.getPackageManager();
        Intent intent = new Intent();
        intent = packageManager.getLaunchIntentForPackage(pkgName);
        if (intent == null) {
            return false;
        } else {
            mContext.startActivity(intent);
            return true;
        }
    }

    private void launch(String url, String name) {
        Intent intent = new Intent(mContext, BrowserActivity.class);
        intent.putExtra(URL, url);
        intent.putExtra("title", url);
        mContext.startActivity(intent);
    }


}
